﻿using System;
using System.Collections.Generic;
using System.Text;
using Clipping.Infrastructure.Interfaces;
using System.Web;
using Clipping.Infrastructure.Models;
using System.Text.RegularExpressions;
namespace Clipping.SearchUrlConstructors
{
    public class ChinaDailyUrlConstructor : ISearchUrlConstruct
    {
        private Encoding _urlEncoding = Encoding.UTF8;
        public Uri ConstructUrl(SearchObject searchObj)
        {
            if (searchObj is TimespanSearchObject)
            {
                TimespanSearchObject so = searchObj as TimespanSearchObject;

                return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), so.StartTime, so.EndTime));

            }
            else if (searchObj is PeriodSearchObject)
            {
                PeriodSearchObject so = searchObj as PeriodSearchObject;
                DateTime now = DateTime.Now;
                switch (so.SearchPeriod)
                {
                    case SearchPeriod.全部时间:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate=&strFromdate=&usedate=&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding)));
                    case SearchPeriod.最近1年:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddYears(-1), now));
                    case SearchPeriod.最近1天:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddDays(-1), now));
                    case SearchPeriod.最近1月:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddMonths(-1), now));
                    case SearchPeriod.最近1周:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddDays(-7), now));
                    case SearchPeriod.最近2天:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddDays(-2), now));
                    case SearchPeriod.最近2月:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddMonths(-2), now));
                    case SearchPeriod.最近3天:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddDays(-3), now));
                    case SearchPeriod.最近3月:
                        return new Uri(string.Format("http://search.chinadaily.com.cn/cn.jsp?searchText={0}&strTodate={2:yyyy-MM-dd}&strFromdate={1:yyyy-MM-dd}&usedate=yes&strAuthor=", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), now.AddMonths(-3), now));
                    default:
                        return null;
                }

            }
            else
                return null;

        }

        public Uri ConstructAbsoluteUrl(Uri baseUri, string url)
        {
            var baseUrl = baseUri.OriginalString;
            if (Regex.Match(baseUrl, @"&page=\d+", RegexOptions.IgnoreCase).Success)
            {
                return new Uri(Regex.Replace(baseUrl, @"&page=\d+", "&page=" + url, RegexOptions.IgnoreCase));
            }
            else
            {
                return new Uri(string.Format("{0}&page={1}", baseUrl, url));
            }


        }
    }
}
